/**
 * 
 */
package com.mobile2you.nci.android;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.util.Linkify;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.mobile2you.nci.android.data.InitialDataLoader;
import com.mobile2you.nci.android.data.SqlHelper;
import com.mobile2you.nci.android.data.WebServicesRequest;
import com.mobile2you.nci.android.domain.News;
import com.mobile2you.nci.android.utils.ListNewsAdapter;
import com.mobile2you.nci.android.utils.LoadImagePool;
import com.mobile2you.nci.android.view.LoaderView;

/**
 * @author JBromo
 * 
 */
public class MenuNews extends Activity implements Callback {

	List<News> listNews = new ArrayList<News>();
	private ListView listView;

	ViewFlipper viewFlipper;

	private ListNewsAdapter adapter;

	TextView titleNews;

	LoaderView loaderView;

	private class LoadCentresTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {

			if (listNews.isEmpty()) {
				listNews = InitialDataLoader.getNews(MenuNews.this);
			}
			listNews = SqlHelper.getInstance(getApplicationContext())
					.getLocalNews();

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);

		}

	}

	private class UpdateLoadCentresTask extends
			AsyncTask<String, String, List<News>> {

		@Override
		protected List<News> doInBackground(String... params) {
			boolean toupdate = SqlHelper.getInstance(MenuNews.this).toUpdate(
					SqlHelper.KEY_NEWS);
			if (toupdate) {
				listNews = WebServicesRequest.getNews(MenuNews.this);
			}
			if (listNews == null || listNews.isEmpty()) {
				listNews = SqlHelper.getInstance(getApplicationContext())
						.getLocalNews();
				if (listNews.isEmpty()) {
					listNews = InitialDataLoader.getNews(MenuNews.this);
				}
			}
			return listNews;
		}

		@Override
		protected void onPostExecute(List<News> result) {
			super.onPostExecute(result);
			if (result != null && !result.isEmpty()) {
				listNews = result;
				showListview();
				viewFlipper.removeView(loaderView);
			} else {
				loaderView.setErrorMessage();
			}

		}

	}

	@Override
	public boolean handleMessage(Message msg) {

		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_news);
		listView = (ListView) findViewById(R.id.list_centre);
		titleNews = (TextView) findViewById(R.id.menu_news_title);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				News news = (News) adapter.getItem(arg2);
				showNewsDetail(news);
				viewFlipper.showNext();

			}
		});
		viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		loaderView = new LoaderView(this);
		viewFlipper.addView(loaderView, 0);
		viewFlipper.setDisplayedChild(0);
		new UpdateLoadCentresTask().execute();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (viewFlipper.getDisplayedChild() != 0) {
				titleNews.setText("News");
				viewFlipper.showPrevious();
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	private void showListview() {
		adapter = new ListNewsAdapter(listNews);
		System.out.println("News List :" + listNews.size());
		listView.setAdapter(adapter);

	}

	private void showNewsDetail(final News news) {
		titleNews.setText(news.getTitle());
		TextView name = (TextView) findViewById(R.id.label_news_title);
		final ImageView newImageView = (ImageView) findViewById(R.id.image_news);
		if (news.getBitmap() == null) {
			LoadImagePool.loadImage(this, news.getImageUrl(), new Handler(
					new Callback() {
						@Override
						public boolean handleMessage(Message msg) {
							if (msg.obj != null) {
								Bitmap image = (Bitmap) msg.obj;
								newImageView
										.setBackgroundDrawable(new BitmapDrawable(
												MenuNews.this.getResources(),
												image));
								news.setBitmap(image);

							}
							return false;
						}
					}), false);
		} else {
			newImageView.setBackgroundDrawable(new BitmapDrawable(MenuNews.this
					.getResources(), news.getBitmap()));
		}
		name.setText(news.getTitle());
		TextView detail = (TextView) findViewById(R.id.label_detail_news);
		detail.setText(news.getText());
		Linkify.addLinks(detail, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS
				| Linkify.EMAIL_ADDRESSES);

	}
}
